function initFormData() {
    //格式化时间
    function p(s) {
        return s < 10 ? '0' + s : s;
    }
    function getNow() {
        var date = new Date();
        return  date.getFullYear() + '-' + p(date.getMonth() + 1) + '-' + p(date.getDate()) + ' ' + p(date.getHours()) + ':' + p(date.getMinutes()) + ':' + p(date.getSeconds());
    }

    $(function () {
        $('.form input').val('').css('width','80%');
        $('.form .table_bg .text_value span,font:last').text('');
        if ($('.title').text() == '销售合同评审表' || $('.title').text() == '采购合同评审表') {
            $('.form .table_bg .text_value font').text('');
        }

        //新建表单页面自动加载当前用户姓名、部门、日历等
        $('.form .table_bg .text_td input,.text_value input').each(function () {
            var key = $(this).attr('title');
            var id = $(this).attr('name');
            if (key.indexOf('时间') != -1 || key.indexOf('日期') != -1) {
                if (key.indexOf('申请时间') != -1 || key.indexOf('申请日期') != -1) {
                    $(this).val(getNow());
                }else{
                    $(this).addClass('dateTime');
                    $(this).attr('id',id);  //移动端日期插件需要用到id
                }                
            }
            if (key.indexOf('申请人') != -1 || key.indexOf('经办人') != -1) {
                $(this).val($('#username').val());
            }
            if (key.indexOf('部门') != -1) {
                $(this).val($('#deparment').val());
            }

        })


        if ($('input[name=FLOW_ID]').val() != 0) {
            var reg = /\{(.*?)\}/g;
            var url = $('#url').val();
            var index = layer.load(3, {shade: false});
            $.get(url, function (res) {
                console.log(res)
                var n = 0;
                $('.form .table_bg input,select,textarea').each(function () {
                    //9.27升级后修改
                    if (res.form[n] != 'undefined') {
                        $(this).after('<span>' + res.form[n] + '</span>');
                        $('.feedback span').text('');
                    }
                    $(this).not('.feedback textarea').remove();
                    n++;
                })
                //会签意见 
                if (res.feed[n] != 'undefined') {
                    var html = '';
                    for (var i = 0; i < res.feed.length; i++) {
                        html += '<p>第' + res.feed[i].prcs_id + '步：' + res.feed[i].prcs_name + '&nbsp;&nbsp;' + res.feed[i].user_name + '<br/>' + res.feed[i].content + '&nbsp;&nbsp;' + res.feed[i].edit_time + '</p>';
                    }
                    $('.form .table_bg .text_td').each(function () {
                        if ($(this).text() == '会签意见') {
                            $(this).next().html(html);
                        }
                    })
                }
                layer.close(index);
            })
        } else if ($('input[name=FLOW_ID]').val() == 0 && ($('input[name=isEdit]').val() == 'edit')) {
            var url = $('#url').val();
            var index = layer.load(3, {shade: false});
            $.get(url, function (res) {
                console.log(res)
                var n = 0;
                $('.form .table_bg .text_value input,select,textarea').each(function () {
                    if (res.form[n] != 'undefined' && ($(this).is('select') == false)) {
                        $(this).val(res.form[n])
                        $(this).text(res.form[n]);
                    }
                    $('.form .table_bg .text_value select option').each(function () {
                        if (($(this).val() == res.form[n])) {
                            $(this).attr('selected', true);
                        }
                    })
                    n++;
                })
                $('input[name=FLOW_ID]').val(res.flow_id);
                layer.close(index);
            })
        }
    })

    function isContains(str, substr) {
        // alert(str);
        //  alert(substr)
        return str.indexOf(substr);
    }

    //回退
    $('.back').on('click', function () {
        var flow_id = $('input[name=FLOW_ID]').val();
        var run_id = $('input[name=RUN_ID]').val();
        var prcs_id = $('input[name=PRCS_ID]').val();
        var currPrcs = $('input[name=currPrcs]').val();
        //alert(flow_id);alert(run_id);alert(prcs_id);
        layer.open({
            type: 2,
            title: [
                "选择回退步骤",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: ['80%', '50%'], //宽高
            offset: ['10%', '10%'], //弹出位置 [top,left]
            content: "/index.php/Index/Index/layer/type/2/flow_id/" + flow_id + "/run_id/" + run_id + "/prcs_id/" + prcs_id + "/currPrcs/" + currPrcs,
        })
    })
}

function initDelForm() {
    $('body').on('click', '.delFlow', function () {
        var url = $('#delurl').val() + '?run_id=' + $(this).attr('data-run');
        var prcs = $(this).attr('data-prcs');
        if (prcs != 1) {
            layer.msg('不能删除已执行的流程');
            return;
        }
        layer.confirm('确定删除吗', {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
            var index = layer.load(1, {shade: false});
            $.get(url, function (res) {
                console.log(res);
                layer.msg(res.info);
                location.reload();
            })
        }, function (index) {
            layer.close(index);
        });

    })
}

//删除日志
function initDelDiary() {
    $('body').on('click', '.delDiary', function () {
        var url = $('#delurl').val() + '?diary_id=' + $(this).attr('data-id');
        layer.confirm('确定删除吗', {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
            var index = layer.load(1, {shade: false});
            $.get(url, function (res) {
                console.log(res);
                layer.msg(res.info);
                location.reload();
            })
        }, function (index) {
            layer.close(index);
        });

    })
}

//删除周报
function initDelWeekly() {
    $('body').on('click', '.delWeekly', function () {
        var url = $('#delurl').val() + '?id=' + $(this).attr('data-id');
        layer.confirm('确定删除吗', {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
            var index = layer.load(1, {shade: false});
            $.get(url, function (res) {
                console.log(res);
                layer.msg(res.info);
                location.reload();
            })
        }, function (index) {
            layer.close(index);
        });

    })
}

//删除新闻评论
function initDelComment() {
    $('body').on('click', '.delComment', function () {
        var url = $('#delurl').val() + '?comment_id=' + $(this).attr('data-id');
        layer.confirm('确定删除吗', {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
            var index = layer.load(1, {shade: false});
            $.get(url, function (res) {
                console.log(res);
                layer.msg(res.info);
                location.reload();
            })
        }, function (index) {
            layer.close(index);
        });

    })

    //打开上一篇或下一篇
    $('.otherOne').on('click', function () {
        var _this = $(this);
        var url = $(this).attr('data-url');
        $.get(url, {id: _this.attr('data-id')}, function (res) {
            console.log(res);
            if (res.status == 0) {
                layer.msg(res.info);
            } else {
                _this.attr('data-id', res.id);
                location.href = res.url;
            }
        })
    })
}

//删除新闻
function initDelNews() {
    $('body').on('click', '.delNews', function () {
        var url = $('#delurl').val() + '?news_id=' + $(this).attr('data-id');
        layer.confirm('确定删除吗', {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
            var index = layer.load(1, {shade: false});
            $.get(url, function (res) {
                console.log(res);
                layer.msg(res.info);
                location.reload();
            })
        }, function (index) {
            layer.close(index);
        });

    })
}

function initFlowStep() {
    $('.checkDept').on('click', function () {
        var txt = $(this).attr('data-name');
        var prcs_id = $(this).attr('data-id');
        //var url = $('input[name=prcs_type]').attr('data-url');
        var url = $(this).find('input[name=prcs_type]').attr('data-url');
        if ($(this).children('img').attr('src') == '/Public/Index/img/s2.png') {
            $(this).children('img').attr('src', '/Public/Index/img/s1.png');
            $(this).siblings().children('img').attr('src', '/Public/Index/img/s2.png');
            $(this).children('input').val(1);
            $(this).siblings().children('input').val(0);

            $('#prcs_id').val(prcs_id);
        }
        if (url) {
            getData(txt, prcs_id, url);
        }

    })

    $(function () {
        var txt = $('#prcs_name_html').val();
        var prcs_id = $('#prcs_id_html').val();
        var url = $('#prcs_url_html').val();
        if (url) {
            getData(txt, prcs_id, url)
        }

    })

    function getData(txt, id, url) {
        // alert(txt);alert(id);alert(url)
        $('.stepName').text(txt);
        $('#prcs_id').val(id);
        var index = layer.load(2, {shade: false});
        $.get(url, function (res) {
            console.log(res)
            var len = res.length;
            var html = '';
            if (len == 1) {
                html += '<p class="people main_user"><span class="one">主办人：</span><span><a>' + res[0].user_name + '</a><img class="pointer delOperator" data-id="' + res[0].user_id + '" src="/Public/Index/img/close_red.png" alt="close" width="15" height="15"/>';
                html += '<input type="hidden" name="user_id" value="' + res[0].user_id + '"><input type="hidden" name="user_name" value="' + res[0].user_name + '"></span></p>';
                html += '<p class="other_user"><span class="one">经办人：</span><span><a>' + res[0].user_name + '</a><img class="pointer delOperator" data-id="' + res[0].user_id + '" src="/Public/Index/img/close_red.png" alt="close" width="15" height="15"/>';
                html += '<input type="hidden" name="user_ids[]" value="' + res[0].user_id + '"><input type="hidden" name="user_names[]" value="' + res[0].user_name + '"></span></p>';
            } else if (len > 1 && res[0].top_default == 2 && res[0].auto_type != 0) {
                html += '<p class="people  main_user"><sp= 2 &&an class="one">无主办人</span></p>';
                html += '<p class="other_user"><span class="one">经办人：</span>';
                for (var i = 0; i < res.length; i++) {
                    html += '<span style="margin-right:10px"><a>' + res[i].user_name + '</a><img class="pointer delOperator" data-id="' + res[i].user_id + '" src="/Public/Index/img/close_red.png" alt="close" width="15" height="15" />';
                    html += '<input type="hidden" name="user_ids[]" value="' + res[i].user_id + '"><input type="hidden" name="user_names[]" value="' + res[i].user_name + '"></span>';
                }
                html += '</p>';
            } else if (len > 1 && res[0].top_default == 2 && res[0].auto_type == 0) {
                html += '<p class="people  main_user"><sp= 2 &&an class="one">无主办人</span></p>';
                html += '<p class="other_user"><span class="one">经办人：</span></p>';
            } else if (len > 1 && res[0].top_default == 0 && res[0].auto_type == 0) {
                html += '<p class="people main_user"><span class="one">主办人：</span></p>';
                html += '<p class="other_user"><span class="one">经办人：</span></p>';
            }

            if (html) {
                $('.operator').html(html);
            } else {
                html = '<p class="people main_user"><span class="one">主办人：</span></p><p class="other_user"><span class="one">经办人：</span></p>';
                $('.operator').html(html);
            }
            layer.close(index);
        })
    }

    //打开部门弹窗
    $('.checkUser').on('click', function () {
        var prcs_id = $('#prcs_id').val();
        var flow_id = $('#flow_id').val();
        var run_id = $('#run_id').val();
        var auto_type = $('#auto_type').val();
        layer.open({
            type: 2,
            title: [
                "选择人员",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: ['80%', '60%'], //宽高
            offset: ['10%', '10%'], //弹出位置 [top,left]
            content: "/index.php/Index/Index/layer/type/1/flow_id/" + flow_id + "/run_id/" + run_id + "/prcs_id/" + prcs_id + "/auto_type/" + auto_type
        })
    })

    //删除主办人或经办人
    $('body').on('click', '.delOperator', function () {
        $(this).parent('span').remove();
    })

    //清空主办人和经办人
    $('.clear-user').on('click', function () {
        $('.main_user span:not(:first)').remove();
        $('.other_user span:not(:first)').remove();
    })

    //转交一下步
    $('.sure').on('click', function () {
        if ($('.operator .main_user .one').text() != '无主办人') {
            if ($('.operator .main_user span:last a').text() == '' || ($('.operator .other_user span:last a').text() == '')) {
                layer.msg('未选择主办人和经办人');
                return false;
            }
        }
        $(this).addClass('btn-submit');
        $('.btn-submit').click();

    })

    //结束流程
    $('.endFlow').on('click', function () {
        var data = $('form').serialize();
        var url = $(this).attr('data-url');
        layer.confirm('确定要结束该流程吗', {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
            var index = layer.load(2, {shade: false});
            $.post(url, data, function (res) {
                console.log(res);
                layer.msg(res.info);
                location.href = res.url;
            })
        }, function (index) {
            layer.close(index);
        });

    })
}




